package com.quanshu.exchange.ui.gui.message.chat.redpackage;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baselibrary.util.glide.GlideUtils;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.db.UserDao;
import com.quanshu.exchange.support.entity.RPInfoBean;
import com.quanshu.exchange.support.entity.RobMemberBean;
import com.quanshu.exchange.support.entity.UserEntity;
import com.quanshu.exchange.support.utils.AppUtils;
import com.quanshu.exchange.support.utils.DateUtil;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.base.recycler.BaseViewHolder;
import com.quanshu.exchange.ui.base.recycler.DataDisplayer;
import com.quanshu.exchange.ui.base.recycler.LayoutWrapper;
import com.quanshu.exchange.ui.base.recycler.SingleAdapter;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RecordResultActivity extends BaseActivity {

    @BindView(R.id.back)
    TextView back;
    @BindView(R.id.record)
    TextView record;
    @BindView(R.id.red_package_name)
    TextView redPackageName;
    @BindView(R.id.red_package_points)
    TextView redPackagePoints;
    @BindView(R.id.fromAvatar)
    ImageView fromAvatar;
    @BindView(R.id.fromName)
    TextView fromName;
    @BindView(R.id.result_info)
    TextView resultInfo;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.red_package_points_layout)
    RelativeLayout redPackagePointsLayout;

    private RPInfoBean rpInfoBean;
    private List<RobMemberBean> robMemberList;
    private RobMemberBean myRobInfo;
    private SingleAdapter<RobMemberBean> adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rob_rp_result);
        ButterKnife.bind(this);
        initIntent();
        initView();
        initListener();
        initData();
    }

    private void initIntent() {
        rpInfoBean = (RPInfoBean) getIntent().getSerializableExtra("RPInfoBean");
        robMemberList = (List<RobMemberBean>) getIntent().getSerializableExtra("List<RobMemberBean>");
        myRobInfo = (RobMemberBean) getIntent().getSerializableExtra("MyRobBean");
    }

    private void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SingleAdapter<>(new DataDisplayer<RobMemberBean>(this, R.layout.item_red_package_record) {
            @Override
            public void bindData(BaseViewHolder holder, LayoutWrapper<RobMemberBean> wrapper, final RobMemberBean item, final int position) {

                UserEntity userEntity = UserDao.getInstance().query(item.getMemberid());
                if (userEntity != null) {
                    holder.setText(R.id.red_package_name, AppUtils.getUserName(userEntity));
                } else {
                    holder.setText(R.id.red_package_name, item.getNickname());
                }
                double d = Double.valueOf(item.getGet_money());
                DecimalFormat decimal_format = new DecimalFormat("0.00");
                holder.setText(R.id.red_package_sum, decimal_format.format(d) + "  积分");
                holder.setText(R.id.red_package_time, DateUtil.TimeStamp2Date(item.getGet_time() + "", "MM-dd HH:mm"));
            }
        });
        recyclerView.setAdapter(adapter);
    }

    private void initListener() {

        RxViewHelper.getInstance().clicks(back, aVoid -> finish());
        RxViewHelper.getInstance().clicks(record, aVoid -> doClickRecord());

    }

    private void initData() {
        if (rpInfoBean != null) {
            redPackageName.setText(rpInfoBean.getContent());
            GlideUtils.setRoundImageView(this, rpInfoBean.getPhoto(), R.drawable.default_avatar, fromAvatar);
            fromName.setText(rpInfoBean.getNickname() + "发的红包");
            int num1 = rpInfoBean.getNumber() - rpInfoBean.getResidue_num();
            BigDecimal b1 = new BigDecimal(rpInfoBean.getMoney()).setScale(2, BigDecimal.ROUND_HALF_UP);
            BigDecimal b2 = new BigDecimal(rpInfoBean.getResidue()).setScale(2, BigDecimal.ROUND_HALF_UP);
            double num2 = b1.subtract(b2).doubleValue();
            resultInfo.setText("已领取" + num1 + "/" + rpInfoBean.getNumber() + "个,共" +
                    num2 + "/" + rpInfoBean.getMoney() + "积分");
        }
        if (myRobInfo == null) {
            redPackagePointsLayout.setVisibility(View.GONE);
        } else {
            redPackagePointsLayout.setVisibility(View.VISIBLE);
            redPackagePoints.setText(myRobInfo.getGet_money());
        }
        if (robMemberList != null) {
            adapter.setData(robMemberList);
        }
    }

    private void doClickRecord() {
        Intent intent = new Intent(this, RedPackageRecordActivity.class);
        startActivity(intent);
    }
}
